<?php

class UserPrivatePageController extends Controller
{
	public $layout='//layouts/column2';

	public function actionIndex()
	{
		$this->render('index');
	}

	public function actionChangePassword()
	{
		// renders the view file 'protected/views/site/index.php'
		// using the default layout 'protected/views/layouts/main.php'
		if(Yii::app()->user->isGuest){
			// If the user is guest or not logged in redirect to the login form
			$this->redirect(array('site/login'));
		}
		else{
			$model = new User;
			$this->render('changePassword',array('model'=>$model,));
		}
	}

	public function actionUpdatePassword()
	{
		if(isset($_POST['User'])){
			$model = new User;
			$model->attributes=$_POST['User'];
			$current_name = Yii::app()->user->name;
			$model1 = User::model()->findByPk($current_name);
			$current_password = $model1->password;

			$old_pwd = $model->old_pwd;
			if($current_password == $old_pwd)
			{
				$new_pwd = $model->pwd;
				User::model()->updateByPk($current_name,array('password'=>$new_pwd));
				$this->render('successChangePassword');
			}
			else
			{
				$model->validate(array('pwd','pwd_repeat','old_pwd'),true);
				$this->render('changePassword',array('model'=>$model,));
			}
		}
	}
	public function actionChangeEmail()
	{
		if(Yii::app()->user->isGuest){
			// If the user is guest or not logged in redirect to the login form
			$this->redirect(array('site/login'));
		}
		else{
			$model = User::model()->findByPk(Yii::app()->user->name);
			$this->render('changeEmail',array('model'=>$model,));
		}
	}

	public function actionUpdateEmail()
	{
		if(isset($_POST['User'])){
			$model = new User;
			$model->attributes=$_POST['User'];
			$current_name = Yii::app()->user->name;
			$model1 = User::model()->findByPk($current_name);
			$new_email = $model->new_email;
			User::model()->updateByPk($current_name,array('email'=>$new_email));
			$this->render('successChangeEmail');
		}
		else
		{
			$this->render('changeEmail',array('model'=>$model,));
		}
	}

	public function actionShowUserInfo()
	{
		// renders the view file 'protected/views/site/index.php'
		// using the default layout 'protected/views/layouts/main.php'
		if(Yii::app()->user->isGuest){
			// If the user is guest or not logged in redirect to the login form
			$this->redirect(array('site/login'));
		}
		else{
			$model = User::model()->findByPk(Yii::app()->user->name);
			$id =$model->id;
			$criteria = new CDbCriteria;
			$criteria->alias="book";
			$criteria->join="INNER JOIN user ON user.ID = book.iduser";
			$criteria->condition="book.iduser = $id";
				
			$dataProvider = new CActiveDataProvider(Book::model(),
			array(
				'criteria'=>$criteria,
				'pagination'=>array(
       			'pageSize'=>6,
			)));
			$this->render('showUserInfo',array('model'=>$model,'dataProvider'=>$dataProvider,));
		}
	}

	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
	// return the filter configuration for this controller, e.g.:
	return array(
	'inlineFilterName',
	array(
	'class'=>'path.to.FilterClass',
	'propertyName'=>'propertyValue',
	),
	);
	}

	public function actions()
	{
	// return external action classes, e.g.:
	return array(
	'action1'=>'path.to.ActionClass',
	'action2'=>array(
	'class'=>'path.to.AnotherActionClass',
	'propertyName'=>'propertyValue',
	),
	);
	}
	*/
}